/*
*    UNIVERSIDADE DE SÃO PAULO. 
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*    This project was/is sponsored by RNP and FAPESP.
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.eureka.knowledge;

import br.org.groupwareworkbench.collablet.coord.category.Category;
import br.org.groupwareworkbench.collablet.coord.category.CategoryMgrInstance;
import br.org.groupwareworkbench.core.framework.Collablet;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class CopyFromBD {

    private Date lerData(ResultSet rs, String nomeCampo) {
        try {
            return rs.getTime(nomeCampo);
        } catch (SQLException e) {
            return null;
        }
    }
    /*
     * TODO: Este método ainda não funciona direito
     */
    public void copyFromBD(Collablet collablet, int maxMaterias) {
        System.out.println("Inicio DA COPIA Do Banco de Dados!");

        ConnectionMysql con = null;
        Connection dbCon = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            con = new ConnectionMysql();
            dbCon = con.getConnection(ConnectionMysql.EUREKA_DB);
            stmt = dbCon.prepareStatement("SELECT * FROM conhecimento");
            rs = stmt.executeQuery();
            
            CategoryMgrInstance category1Mgr = new CategoryMgrInstance(Collablet.findByName("category1Mgr"));
            CategoryMgrInstance category2Mgr = new CategoryMgrInstance(Collablet.findByName("category2Mgr"));
            //TagMgrInstance tagMgr = new TagMgrInstance(Collablet.findByName("tagMgr"));
            
            Category category = new Category();
            category.setId(new Long(1));
            category.setName("TIC-ES");
            category.setCollablet(category1Mgr.getCollablet());
            category1Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(2));
            category.setName("Oxigênio");
            category.setCollablet(category1Mgr.getCollablet());
            category1Mgr.save(category);

            category = new Category();
            category.setId(new Long(3));
            category.setName("Mentor - Gerência de Incidentes");
            category.setCollablet(category1Mgr.getCollablet());
            category1Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(4));
            category.setName("Mentor - Gerência de Mudança");
            category.setCollablet(category1Mgr.getCollablet());
            category1Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(5));
            category.setName("Não Verificado");
            category.setCollablet(category2Mgr.getCollablet());
            category2Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(6));
            category.setName("Em Avaliação");
            category.setCollablet(category2Mgr.getCollablet());
            category2Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(7));
            category.setName("Resolvido");
            category.setCollablet(category2Mgr.getCollablet());
            category2Mgr.save(category);
            
            category = new Category();
            category.setId(new Long(8));
            category.setName("Nenhum");
            category.setCollablet(category2Mgr.getCollablet());
            category2Mgr.save(category);
            
            for (int i = 0; rs.next() && (maxMaterias == -1 || i < maxMaterias); i++) {
                String codReg = rs.getString("COD_REG");
                
                Date dataPublica = rs.getDate("DATA_CADASTRO");
                int projeto = Integer.parseInt(rs.getString("TX_PROJETO"));
                String area = rs.getString("TX_AREA");
                String causa = rs.getString("TX_CAUSA");
                String solucao = rs.getString("TX_SOLUCAO");
                String chave = rs.getString("TX_CHAVE");
                String titulo = rs.getString("TX_TITULO");
                String status = rs.getString("TX_STATUS");
                int statusKnowledge = 8;
                if (!status.isEmpty())
                    statusKnowledge = Integer.parseInt(status) + 4;

                Knowledge knowledge = new Knowledge();
                knowledge.setCodReg(codReg);
                knowledge.setCause(causa);
                knowledge.setTitle(titulo);
                knowledge.setSolution(solucao);
                knowledge.setArea(area);
                if ( !chave.equals("null"))
                    knowledge.setChave(chave);
                knowledge.setPublishDate(dataPublica);
                knowledge.setCollablet(collablet);
                knowledge.save();
                category1Mgr.saveWidgets( projeto , knowledge );
                category2Mgr.saveWidgets(statusKnowledge, knowledge);
                //tagMgr.saveWidgets(chave, knowledge);
            }
            System.out.println("Fim da cópia da Eureka original.");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("O Banco de Dados original não foi copiado, verifique sua conexão!");
        } finally {
            try {
                if (rs != null) rs.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (dbCon != null) dbCon.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
            try {
                if (con != null) con.close();
            } catch (SQLException e) {
                // TODO
                e.printStackTrace();
            }
        }
    }
}
